1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.collect;
18
19 import static com.google.common.collect.CollectPreconditions.checkEntryNotNull;
20
21 import com.google.common.annotations.GwtCompatible;
22
23 import javax.annotation.Nullable;
24
25
26
27
28
29
30
31 @GwtCompatible(serializable = true, emulated = true)
32 @SuppressWarnings("serial")
33 final class SingletonImmutableBiMap<K, V> extends ImmutableBiMap<K, V> {
34
35 final transient K singleKey;
36 final transient V singleValue;
37
38 SingletonImmutableBiMap(K singleKey, V singleValue) {
39 checkEntryNotNull(singleKey, singleValue);
40 this.singleKey = singleKey;
41 this.singleValue = singleValue;
42 }
43
44 private SingletonImmutableBiMap(K singleKey, V singleValue,
45 ImmutableBiMap<V, K> inverse) {
46 this.singleKey = singleKey;
47 this.singleValue = singleValue;
48 this.inverse = inverse;
49 }
50
51 SingletonImmutableBiMap(Entry<? extends K, ? extends V> entry) {
52 this(entry.getKey(), entry.getValue());
53 }
54
55 @Override public V get(@Nullable Object key) {
56 return singleKey.equals(key) ? singleValue : null;
57 }
58
59 @Override
60 public int size() {
61 return 1;
62 }
63
64 @Override public boolean containsKey(@Nullable Object key) {
65 return singleKey.equals(key);
66 }
67
68 @Override public boolean containsValue(@Nullable Object value) {
69 return singleValue.equals(value);
70 }
71
72 @Override boolean isPartialView() {
73 return false;
74 }
75
76 @Override
77 ImmutableSet<Entry<K, V>> createEntrySet() {
78 return ImmutableSet.of(Maps.immutableEntry(singleKey, singleValue));
79 }
80
81 @Override
82 ImmutableSet<K> createKeySet() {
83 return ImmutableSet.of(singleKey);
84 }
85
86 transient ImmutableBiMap<V, K> inverse;
87
88 @Override
89 public ImmutableBiMap<V, K> inverse() {
90
91 ImmutableBiMap<V, K> result = inverse;
92 if (result == null) {
93 return inverse = new SingletonImmutableBiMap<V, K>(
94 singleValue, singleKey, this);
95 } else {
96 return result;
97 }
98 }
99 }